﻿using Lis.IBLL;
using Lis.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Lis.Web.Controllers
{
    public class UserActionController : Controller
    {
        // GET: UserInfo
        //UserInfoService userInfoService = new UserInfoService();

        //Spring.Net--使用注入的方式

        public IUserCirculateService UserCirculateService { get; set; }

        public ActionResult Index()
        {
            return View();
        }

        #region 01，获取所有用户信息操作+JsonResult ShowAllUser()
        /// <summary>
        /// 获取所有用户流通权限信息操作
        /// </summary>
        /// <returns></returns>
        public JsonResult ShowAllUserAction()
        {
            int pageInde = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 1 : int.Parse(Request["rows"]);

            var userType = Request["userType"] == null ? "本科生" : Request["userType"];

            int totalCount;

            var userActionList = UserCirculateService.GetPageEntities<string>(pageSize, pageInde, out totalCount, true, u => u.DZLX == userType && u.DCDDM != null && (u.TSLXDM.Contains("0001") || u.TSLXDM.Contains("0003")), u => u.DZLX).ToList();

            var temp = from u in userActionList
                       select new
                       {
                           UserType = u.DZLX,
                           DepartmentName = u.GCDWDM,
                           CollectionName = u.DCDDM,
                           BookType = u.TSLXDM,
                           UserDepartmentName = u.DZDWMC
                       };
            //客户端就是遍历rows  jq easy-ui就是遍历rows
            return Json(new { rows = temp, total = totalCount }, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 02，用户权限编辑
        /// <summary>
        /// 用户权限编辑
        /// </summary>
        /// <param name="id">读者类型</param>
        /// <returns></returns>
        public ActionResult EditUserAction()
        {
            string userType = Request["userType"];
            string collectionName = Request["collectionName"];
            UserCirculate model = UserCirculateService.GetEntitiesByUserType(userType, collectionName).First<UserCirculate>();
            return Json(model, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 编辑权限信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditUserAction(UserCirculate model)
        {
            bool exist = false;
            if (ModelState.IsValid)
            {
                UserCirculate userCirculate_model = UserCirculateService.GetEntitiesByUserType(model.DZLX, model.DCDDM).First<UserCirculate>();
                userCirculate_model.DZLX = model.DZLX;
                userCirculate_model.DCDDM = model.DCDDM;
                userCirculate_model.DZDWMC = model.DZDWMC;
                userCirculate_model.TSLXDM = model.TSLXDM;
                userCirculate_model.GCDWDM = model.GCDWDM;
                userCirculate_model.ZDJYCS = model.ZDJYCS;
                userCirculate_model.JYTS = model.JYTS;
                userCirculate_model.XJCS = model.XJCS;
                userCirculate_model.YYCS = model.YYCS;
                userCirculate_model.YYTS = model.YYTS;
                userCirculate_model.BLCS = model.BLCS;
                userCirculate_model.GQFJ = model.GQFJ;
                userCirculate_model.YL1 = model.YL1;
                exist = UserCirculateService.UpDate(userCirculate_model);
            }
            if (exist)
            {
                return Content("ok");
            }
            else
            {
                return Content("false");
            }
        }
        #endregion

        #region 03，添加用户权限信息+JsonResult AddAction(ActionInfo model)
        /// <summary>
        /// 添加用户权限信息
        /// </summary>
        /// <param name="model">实体模型</param>
        /// <returns></returns>
        public JsonResult AddUserAction(UserCirculate model)
        {
            bool exist = false;
            if (ModelState.IsValid)
            {
                model.MKLX = "中文图书";
                model.DZDWMC = "安徽农业大学";
                model.SJDW = "1";
                model.YL1 = "延时0时";
                exist = UserCirculateService.Add(model);
            }
            if (exist)
            {
                return Json("ok");
            }
            else
            {
                return Json("false");
            }
        }
        #endregion

        #region 04，删除用户权限信息+ActionResult Delete()
        /// <summary>
        /// 删除权限信息
        /// </summary>
        /// <param name="strId"></param>
        /// <returns></returns>
        public ActionResult Delete()
        {
            string userType = Request["userType"];
            string collectionName = Request["collectionCode"];
            //string[] strUserType = userType.Split(new char[] { ',' });
            //string[] strCollectionName = collectionName.Split(new char[] { ',' });
            UserCirculate model = UserCirculateService.GetEntitiesByUserType(userType, collectionName).First<UserCirculate>();
            var exist= UserCirculateService.Delete(model);
            if (exist)
            {
                return Content("ok");
            }
            else
            {
                return Content("false");
            }
        }
        #endregion

    }
}